package com.jiantong.work.impl.dao;

import com.google.common.collect.Maps;
import com.jiantong.work.model.ImmunizationSubRecord;
import io.terminus.common.model.Paging;
import io.terminus.common.mysql.dao.MyBatisDao;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mz on 26/7/17.
 */
@Repository
public class ImmunizationSubRecordDao extends MyBatisDao<ImmunizationSubRecord> {


    /**
     * 查询ImmunizationSubRecord list 对应一个主表
     *
     * @param criteria 参数
     * @return List<ImmunizationSubRecord>
     */
    public List<ImmunizationSubRecord> selectList(Map<String, Object> criteria) {
        return getSqlSession().selectList(sqlId("selectList"), criteria);
    }


    public Paging<ImmunizationSubRecord> pagingImmunizationRecordGqv(Integer offset, Integer limit, Map<String, Object> criteria) {
        if (criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = getSqlSession().selectOne(sqlId("countImmunizationRecordGov"), criteria);
        if (total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<ImmunizationSubRecord> data = getSqlSession().selectList(sqlId("pagingImmunizationRecordGov"), criteria);
            return new Paging<>(total, data);
        }
    }

    /**
     * 返回当前单据的编码的最大值
     *
     * @param number 除去流水号的前缀
     * @return string
     */
    public String findCurrentMaxNumber(String number) {
        return getSqlSession().selectOne(sqlId("findCurrentMaxNumber"), number);
    }


    public List<Map<String, Object>> findAlltimesAlltype(Map map) {

        return getSqlSession().selectList(sqlId("findAlltimesAlltype"), map);

    }


    /**
     * 免疫记录的次数
     *
     * @param criteria 参数
     * @return long
     */
    public Long countISRTimes(Map<String, Object> criteria) {
        return getSqlSession().selectOne(sqlId("countISRTimes"), criteria);
    }

    public List<ImmunizationSubRecord> findBybreedFactoryId(Long breedFactoryId) {
        Map map = new HashMap();
        map.put("deleteFlag", 1);
        map.put("breedFactoryId", breedFactoryId);

        return getSqlSession().selectList(sqlId("findBybreedFactoryId"), map);
    }

    public Integer countImmunizationForWeb(Map<String, Object> map) {
        return getSqlSession().selectOne(sqlId("countImmunizationForWeb"),map);
    }

    public List<Map<String , Object>> findImmunizationInfoForWeb(Map<String, Object> map) {
        return getSqlSession().selectList(sqlId("findImmunizationInfoForWeb"),map);
    }
}
